package com.itheima.controller;

import com.itheima.domain.vo.Result;
import com.itheima.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-08-16 16:36
 */
@RestController
@RequestMapping("/upload")
@Slf4j
public class FilesUploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传{}",file);
        if (ObjectUtils.isEmpty(file)){
            return Result.error("上传失败");
        }
        // 生成唯一文件名
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uniqueFileName = UUID.randomUUID().toString().replace("-", "") + extName;
        // 上传文件
        String url = aliyunOSSOperator.upload(file.getBytes(), uniqueFileName);
        return Result.success(url);
    }
}
